package cn.wolfcode.car.business.web.controller.system;

import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("business/statementItem")
public class StatementItemController {

    @Autowired
    private IStatementService statementService;
    @Autowired
    private IStatementItemService statementItemService;
    @Autowired
    private IUserService userService;


    //模板前缀
    private static final String prefix = "/business/statementItem/";

    @RequestMapping("/itemDetail")
    public String itemDetail(Long statementId, Model model){
        //1.根据结算单id查询结算单对象
        Statement statement = statementService.get(statementId);
        model.addAttribute("statement",statement);
        if(Statement.STATUS_CONSUME.equals(statement.getStatus())){
            //消费中
            return prefix + "itemAdd";
        }else {
            //获取收款人
            Long payeeId = statement.getPayeeId();
            User payee = userService.get(payeeId);
            statement.setPayee(payee);
            //已支付
            return prefix + "itemDetail";
        }

    }

    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<StatementItem> query(StatementItemQuery qo){
        return statementItemService.query(qo);
    }


    @RequestMapping("/saveItems")
    //@RequestBody   把浏览器请求发送的Json数据注入到对象中
    @ResponseBody  //把对象解析成Json字符串响应到浏览器
    public AjaxResult saveItems(@RequestBody List<StatementItem> items){
        statementItemService.saveItems(items);
        return AjaxResult.success();
    }



    @RequestMapping("/payStatement")
    //@RequestBody   把浏览器请求发送的Json数据注入到对象中
    @ResponseBody  //把对象解析成Json字符串响应到浏览器
    public AjaxResult payStatement(Long statementId){
        statementItemService.payStatement(statementId);
        return AjaxResult.success();
    }






}
